#include <allegro.h>
#include <ctype.h>
#include "wingfx.h"
#include "main.h"

void window_draw (BITMAP* target, Window* window)
{
	int sx_offset = window->set * GUI_OFFSET; // Source x offset for everything
	
	masked_blit(window->setbmp, target, sx_offset,                           0, window->x                             , window->y                             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(window->setbmp, target, sx_offset + 2*GUI_TILE_W,            0, window->x + (window->w-1) * GUI_TILE_W, window->y                             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(window->setbmp, target, sx_offset,                2*GUI_TILE_H, window->x                             , window->y + (window->h-1) * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	masked_blit(window->setbmp, target, sx_offset + 2*GUI_TILE_W, 2*GUI_TILE_H, window->x + (window->w-1) * GUI_TILE_W, window->y + (window->h-1) * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	for (int x = 1; x < window->w-1; x++) {
		masked_blit(window->setbmp, target, sx_offset + GUI_TILE_W, 0,            window->x + x * GUI_TILE_W, window->y                             , GUI_TILE_W, GUI_TILE_H);		
		masked_blit(window->setbmp, target, sx_offset + GUI_TILE_W, 2*GUI_TILE_H, window->x + x * GUI_TILE_W, window->y + (window->h-1) * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);		
	}
	for (int y = 1; y < window->h-1; y++) {
		masked_blit(window->setbmp, target, sx_offset,                GUI_TILE_H, window->x,                              window->y + y * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
		masked_blit(window->setbmp, target, sx_offset + 2*GUI_TILE_W, GUI_TILE_H, window->x + (window->w-1) * GUI_TILE_W, window->y + y * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	}
	for (int x = 1; x < window->w-1; x++)
	for (int y = 1; y < window->h-1; y++) {
		masked_blit(window->setbmp, target, sx_offset + GUI_TILE_W, GUI_TILE_H, window->x + x * GUI_TILE_W, window->y + y * GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);		
	}
}

void button_draw (BITMAP* target, Button* button, int highlight)
{
	int sx_offset = button->set * GUI_OFFSET; // Source x offset for everything
	
	int source_y = 3 * GUI_TILE_H;
	if (button->pressed) {
		source_y = 5 * GUI_TILE_H;
	} else
	if (highlight) {
		source_y = 9 * GUI_TILE_H;
	}
	
	masked_blit(button->setbmp, target, sx_offset,                source_y,              button->x                             , button->y             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(button->setbmp, target, sx_offset + 2*GUI_TILE_W, source_y,              button->x + (button->w-1) * GUI_TILE_W, button->y             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(button->setbmp, target, sx_offset,                source_y + GUI_TILE_H, button->x                             , button->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	masked_blit(button->setbmp, target, sx_offset + 2*GUI_TILE_W, source_y + GUI_TILE_H, button->x + (button->w-1) * GUI_TILE_W, button->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	for (int x = 1; x < button->w-1; x++) {
		masked_blit(button->setbmp, target, sx_offset + GUI_TILE_W, source_y,              button->x + x * GUI_TILE_W, button->y             , GUI_TILE_W, GUI_TILE_H);		
		masked_blit(button->setbmp, target, sx_offset + GUI_TILE_W, source_y + GUI_TILE_H, button->x + x * GUI_TILE_W, button->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);		
	}
	textout_centre_ex(target, button->font, button->caption, button->x + button->w * GUI_TILE_W / 2 + button->pressed, button->y +2 + button->pressed, -1, -1);
}

void textfield_draw (BITMAP* target, Textfield* textfield, int cursor)
{
	int sx_offset = textfield->set * GUI_OFFSET; // Source x offset for everything
	
	int source_y = 7 * GUI_TILE_H;
	
	masked_blit(textfield->setbmp, target, sx_offset,                source_y,              textfield->x                                , textfield->y             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(textfield->setbmp, target, sx_offset + 2*GUI_TILE_W, source_y,              textfield->x + (textfield->w-1) * GUI_TILE_W, textfield->y             , GUI_TILE_W, GUI_TILE_H);
	masked_blit(textfield->setbmp, target, sx_offset,                source_y + GUI_TILE_H, textfield->x                                , textfield->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	masked_blit(textfield->setbmp, target, sx_offset + 2*GUI_TILE_W, source_y + GUI_TILE_H, textfield->x + (textfield->w-1) * GUI_TILE_W, textfield->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);
	for (int x = 1; x < textfield->w-1; x++) {
		masked_blit(textfield->setbmp, target, sx_offset + GUI_TILE_W, source_y,              textfield->x + x * GUI_TILE_W, textfield->y             , GUI_TILE_W, GUI_TILE_H);		
		masked_blit(textfield->setbmp, target, sx_offset + GUI_TILE_W, source_y + GUI_TILE_H, textfield->x + x * GUI_TILE_W, textfield->y + GUI_TILE_H, GUI_TILE_W, GUI_TILE_H);		
	}
	textout_ex(target, textfield->font, textfield->caption, textfield->x + 6, textfield->y +2, -1, -1);
	if (cursor) {
		int cx = textfield->x + 7 + text_length(textfield->font, textfield->caption);
		vline (target, cx, textfield->y+3, textfield->y+12, makecol(0,0,0));
	}
}

void button_mouseevent (Button* button, int mouse_x, int mouse_y, int mouse_button)
{
	if (mouse_button) {
		if ((mouse_x >= button->x) && (mouse_x < button->x + button->w * GUI_TILE_W) &&
		    (mouse_y >= button->y) && (mouse_y < button->y + 2 * GUI_TILE_H)) {
				button->pressed = 1;
		} else {
				button->pressed = 0;
		}
	} else {
		if (button->pressed) button->clicked = 1;
		button->pressed = 0;
	}
}

int button_was_clicked (Button* button)
{
	if (button->clicked) {
		button->clicked = 0;
		return 1;
	} else {
		return 0;
	}
}

void textfield_type (Textfield* textfield, char c)
{
	int len = strlen(textfield->caption);
	
	if (c == '\b') {
		if (len > 0) {
			textfield->caption[len-1] = '\0';
		}
	} else
	
	if (isprint(c) && (len < (GUI_CAPTION_MAXLEN-1))) {
		textfield->caption[len] = c;
		textfield->caption[len+1] = '\0';
	}
}

int textfield_is_clicked (Textfield* textfield, int mouse_x, int mouse_y, int mouse_button)
{
	if (mouse_button) {
		if ((mouse_x >= textfield->x) && (mouse_x < textfield->x + textfield->w * GUI_TILE_W) &&
		    (mouse_y >= textfield->y) && (mouse_y < textfield->y + 2 * GUI_TILE_H)) {
				return 1;
		}
	}
	
	return 0;
}

void get_mouse (int* x, int* y)
{
	*x = mouse_x / SCALE_X;
	*y = mouse_y / SCALE_Y;
}

#ifdef TEST_WINGFX

// ========= MAIN ROUTINE DOWN HERE ==============

BITMAP* buffer;
FONT* myfont;
int quit;
BITMAP* global_window_set;

Window win1;
Button btn1;

void setup_titlescreen () 
{
	win1 = (Window) { global_window_set, 0, 30, 30, 10, 7 };
	btn1 = (Button) { global_window_set, 0, 50, 60, 4, 0, myfont, "hello" };
}
		
void draw_state()
{
	window_draw (buffer, &win1);
	button_draw (buffer, &btn1);
	blit (global_window_set, buffer, 0, 0, 300, 100, global_window_set->w, global_window_set->h);
}
			
void flip()
{
	stretch_blit(buffer, screen, 0, 0, buffer->w, buffer->h, 0, 0, SCREEN_W, SCREEN_H);
}

volatile int ticks = 0; 
void timer(void)
{
	ticks++;
}

void load_allegro ()
{
	allegro_init();
	install_keyboard();
	install_timer();
	set_color_depth(32);
	set_alpha_blender();
	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
	buffer = create_bitmap(SCREEN_W/2, SCREEN_H/2);
	install_int_ex(timer, BPS_TO_TIMER(60));
}

void main_loop ()
{
        ticks = 0;
        while (!key[KEY_ESC]) {
			draw_state();
			flip();
			rest(50);
        }
}

int main () {
        load_allegro ();
		myfont = load_font("sans.tga", NULL, NULL);
        make_trans_font(myfont);
		global_window_set = load_bitmap("gfx/gui.tga", NULL);
        
        setup_titlescreen ();
        printf ("status report!\n");
        printf ("myfont is null? %s\n", (myfont == NULL) ? "True" : "False");
        printf ("button font is myfont? %s\n", (btn1.font == myfont) ? "True" : "False");
        printf ("color font? %s\n", is_color_font(btn1.font) ? "True" : "False");
        printf ("mono font? %s\n", is_mono_font(btn1.font) ? "True" : "False");
        
        main_loop ();

        return EXIT_SUCCESS;
}
END_OF_MAIN()

#endif /* TEST_WINGFX */
